* This file contains the replication details for Tables and Figures in JOP, ms#200640
* This do-file should be run on the data JOP_200640.dta

* Select the details of the time-series and cross-sectional elements of the data
xtset bonicaridDT_num yearDT
set more off

*******************************************************
*** JOP MAIN MODELS, TABLES, FIGURES AND ROBUSTNESS ***
*******************************************************

* TABLE 1: QUALITY CHALLENGERS
* MODEL 1.1: 
zip sumqualchall expectation pripct_lag incumptyadvan cfscore_ideologue gender power allleader termlimit i.yearDT, cluster(bonicaridDT) inflate(_cons)
* MODEL 1.2: 
zip sumqualchall i.expectation pripct_lag incumptyadvan cfscore_ideologue gender power allleader termlimit i.yearDT, cluster(bonicaridDT) inflate(_cons)
* MODEL 1.3:
zip sumqualchall expectation pripct_lag incumptyadvan cfscore_ideologue gender power allleader termlimit i.yearDT if priuo!=1, cluster(bonicaridDT) inflate(_cons)
* MODEL 1.4: 
zip sumchall15pct expectation pripct_lag incumptyadvan cfscore_ideologue gender power allleader termlimit i.yearDT if priuo!=1, cluster(bonicaridDT) inflate(_cons)

*Figure 1: showing expected values and confidence intervals for expectation values, based on Model 1.2, showing that big boost is from "Below Expectations" 
* Figure 1a
zip sumqualchall i.expectation pripct_lag incumptyadvan cfscore_ideologue gender power allleader termlimit i.yearDT, cluster(bonicaridDT) inflate(_cons)
margins, atmeans at(gender=0 power=0 allleader=0 termlimit=0 expectation=(1(1)3))
marginsplot, scheme(s1manual) recast(scatter)  ///
	yscale(range(0.02 0.085)) ylabel(0.02(.02)0.08, angle(0))  ytitle("Number of Quality Challengers", size(medium)) plotopts(msize(small))  ///
	xscale(range(0.5 3.5)) xlabel(1 "Below Expectations" 2 "Meets Expectations" 3 "Exceeds Expectations") ///
	xtitle("Legislative Effectiveness", size(large)) xscale(titlegap(5)) ///
	title(" ", size(large)) plotregion(style(none))
* Figure 1b
zip sumchall15pct expectation pripct_lag incumptyadvan cfscore_ideologue gender power allleader termlimit i.yearDT if priuo!=1, cluster(bonicaridDT) inflate(_cons)
margins, atmeans at(gender=0 power=0 allleader=0 termlimit=0 expectation=(1(1)3))
marginsplot, scheme(s1manual) recast(scatter)  ///
	yscale(range(0.10 0.30)) ylabel(0.10(.05)0.30, angle(0))  ytitle("Number of Viable Challengers", size(medium)) plotopts(msize(small))  ///
	xscale(range(0.5 3.5)) xlabel(1 "Below Expectations" 2 "Meets Expectations" 3 "Exceeds Expectations") xtitle("Legislative Effectiveness", size(large)) xscale(titlegap(5)) ///
	title(" ", size(large)) plotregion(style(none))


* TABLE 2: WINNING PRIMARY
* MODEL 2.1: 
logit priwin expectation maxqualchall pripct_lag incumptyadvan cfscore_ideologue gender power allleader termlimit i.yearDT, cluster(bonicaridDT) 
* MODEL 2.2: 
logit priwin i.expectation maxqualchall pripct_lag incumptyadvan cfscore_ideologue gender power allleader termlimit i.yearDT, cluster(bonicaridDT) 
* MODEL 2.3: 
logit priwin les maxqualchall pripct_lag incumptyadvan cfscore_ideologue gender power allleader termlimit i.yearDT, cluster(bonicaridDT) 
* MODEL 2.4: 
logit priwin expectation maxqualchall pripct_lag incumptyadvan cfscore_ideologue gender power allleader termlimit i.yearDT if priuo!=1, cluster(bonicaridDT)

* TABLE 3: INTERACTIONS ON NUMBER OF CHALLENGERS
* MODEL 3.1: 
logit priwin c.expectation##c.nchallengers_sameparty pripct_lag incumptyadvan cfscore_ideologue gender power allleader termlimit i.yearDT, cluster(bonicaridDT) 
* MODEL 3.2: 
logit priwin i.expectation##c.nchallengers_sameparty pripct_lag incumptyadvan cfscore_ideologue gender power allleader termlimit i.yearDT, cluster(bonicaridDT) 
* MODEL 3.3: 
logit priwin c.les##c.nchallengers_sameparty pripct_lag incumptyadvan cfscore_ideologue gender power allleader termlimit i.yearDT, cluster(bonicaridDT) 
* MODEL 3.4: 
logit priwin c.expectation##c.nchallengers_sameparty pripct_lag incumptyadvan cfscore_ideologue gender power allleader termlimit i.yearDT if priuo!=1, cluster(bonicaridDT)

* TABLE 4: PROBABILITY OF PRIMARY DEFEAT BY EFFECTIVENESS AND NUMBER OF CHALLENGERS (Based on Model 3.4)
logit prilose c.expectation##c.nchallengers_sameparty pripct_lag incumptyadvan cfscore_ideologue gender power allleader termlimit i.yearDT if priuo!=1, cluster(bonicaridDT)
* FOR ONE PRIMARY CHALLENGER:
margins, atmeans at(gender=0 power=0 allleader=0 expectation=(1 2 3) nchallengers_sameparty=(1))
* FOR TWO PRIMARY CHALLENGERS:
margins, atmeans at(gender=0 power=0 allleader=0 expectation=(1 2 3) nchallengers_sameparty=(2))
* FOR THREE PRIMARY CHALLENGERS:
margins, atmeans at(gender=0 power=0 allleader=0 expectation=(1 2 3) nchallengers_sameparty=(3))
	

***************************************
*** SUPPLEMENTAL APPENDIX MATERIALS ***
***************************************

* TABLE A1: Summary statistics table 
sum expectation sumqualchall sumchall15pct pripct_lag incumptyadvan cfscore_ideologue gender power allleader termlimit nchallengers_sameparty seniority majority comsubchair priwin

* TABLE A2
* MODEL A2.1: 
zip sumqualchall seniority majority comsubchair power allleader pripct_lag incumptyadvan cfscore_ideologue gender termlimit i.yearDT, cluster(bonicaridDT) inflate(_cons)
* MODEL A2.2: 
zip sumqualchall seniority majority comsubchair power allleader pripct_lag incumptyadvan cfscore_ideologue gender termlimit i.yearDT if priuo!=1, cluster(bonicaridDT) inflate(_cons)
* MODEL A2.3: 
zip sumchall15pct seniority majority comsubchair power allleader pripct_lag incumptyadvan cfscore_ideologue gender termlimit i.yearDT if priuo!=1, cluster(bonicaridDT) inflate(_cons)

* TABLE A3
* MODEL A3.1: 
zip sumqualchall expectation seniority majority comsubchair power allleader pripct_lag incumptyadvan cfscore_ideologue gender termlimit i.yearDT, cluster(bonicaridDT) inflate(_cons)
* MODEL A3.2:
zip sumqualchall i.expectation seniority majority comsubchair power allleader pripct_lag incumptyadvan cfscore_ideologue gender termlimit i.yearDT, cluster(bonicaridDT) inflate(_cons)
* MODEL A3.3: 
zip sumqualchall expectation seniority majority comsubchair power allleader pripct_lag incumptyadvan cfscore_ideologue gender termlimit i.yearDT if priuo!=1, cluster(bonicaridDT)inflate(_cons)
* MODEL A3.4: 
zip sumchall15pct expectation seniority majority comsubchair power allleader pripct_lag incumptyadvan cfscore_ideologue gender termlimit i.yearDT if priuo!=1, cluster(bonicaridDT)inflate(_cons)

* Table A4
* MODEL A4.1: 
zip sumqualchall expectation seniority sensq majority comsubchair power allleader pripct_lag incumptyadvan cfscore_ideologue gender termlimit i.yearDT, cluster(bonicaridDT) inflate(_cons)
* MODEL A4.2:
zip sumqualchall expectation c.seniority##termlimit majority comsubchair power allleader pripct_lag incumptyadvan cfscore_ideologue gender i.yearDT, cluster(bonicaridDT) inflate(_cons)
* MODEL A4.3:
zip sumqualchall expectation seniority sensq majority comsubchair power allleader pripct_lag incumptyadvan cfscore_ideologue gender termlimit i.yearDT if priuo!=1, cluster(bonicaridDT)inflate(_cons)
* MODEL A4.4: 
zip sumqualchall expectation c.seniority##termlimit majority comsubchair power allleader pripct_lag incumptyadvan cfscore_ideologue gender i.yearDT if priuo!=1, cluster(bonicaridDT)inflate(_cons)

* Table A5: 
* MODEL A5.1: 
nbreg sumqualchall expectation pripct_lag incumptyadvan cfscore_ideologue gender power allleader termlimit i.yearDT, cluster(bonicaridDT)
* MODEL A5.2:
nbreg sumqualchall i.expectation pripct_lag incumptyadvan cfscore_ideologue gender power allleader termlimit i.yearDT, cluster(bonicaridDT)
* MODEL A5.3:
nbreg sumqualchall expectation pripct_lag incumptyadvan cfscore_ideologue gender power allleader termlimit i.yearDT if priuo!=1, cluster(bonicaridDT)
* MODEL A5.4: 
nbreg sumchall15pct expectation pripct_lag incumptyadvan cfscore_ideologue gender power allleader termlimit i.yearDT if priuo!=1, cluster(bonicaridDT)

* TABLE A6: 
* MODEL A6.1: 
xtreg sumqualchall expectation pripct_lag incumptyadvan cfscore_ideologue gender power allleader termlimit i.yearDT, cluster(bonicaridDT)
* MODEL A6.2:
xtreg sumqualchall i.expectation pripct_lag incumptyadvan cfscore_ideologue gender power allleader termlimit i.yearDT, cluster(bonicaridDT)
* MODEL A6.3:
xtreg sumqualchall expectation pripct_lag incumptyadvan cfscore_ideologue gender power allleader termlimit i.yearDT if priuo!=1, cluster(bonicaridDT)
* MODEL A6.4: 
xtreg sumchall15pct expectation pripct_lag incumptyadvan cfscore_ideologue gender power allleader termlimit i.yearDT if priuo!=1, cluster(bonicaridDT)

* TABLE A7: 
* MODEL A7.1: 
zip sumqualchall expectation pripct_lag incumptyadvan cfscore_ideologue gender power allleader termlimit i.yearDT if yearDT<1995, cluster(bonicaridDT) inflate(_cons)
* MODEL A7.2: 
zip sumqualchall expectation pripct_lag incumptyadvan cfscore_ideologue gender power allleader termlimit i.yearDT if yearDT>1995, cluster(bonicaridDT) inflate(_cons)

* TABLE A8
* MODEL A8.1: 
logit priwin seniority majority comsubchair power allleader maxqualchall pripct_lag incumptyadvan cfscore_ideologue gender termlimit i.yearDT, cluster(bonicaridDT) 
* MODEL A8.2: 
logit priwin seniority majority comsubchair power allleader maxqualchall pripct_lag incumptyadvan cfscore_ideologue gender termlimit i.yearDT if priuo!=1, cluster(bonicaridDT)

* TABLE A9: 
* MODEL A9.1: 
logit priwin expectation seniority majority comsubchair power allleader maxqualchall pripct_lag incumptyadvan cfscore_ideologue gender termlimit i.yearDT, cluster(bonicaridDT) 
* MODEL A9.2: 
logit priwin i.expectation seniority majority comsubchair power allleader maxqualchall pripct_lag incumptyadvan cfscore_ideologue gender termlimit i.yearDT, cluster(bonicaridDT) 
* MODEL A9.3: 
logit priwin les seniority majority comsubchair power allleader maxqualchall pripct_lag incumptyadvan cfscore_ideologue gender termlimit i.yearDT, cluster(bonicaridDT) 
* MODEL A9.4: 
logit priwin expectation seniority majority comsubchair power allleader maxqualchall pripct_lag incumptyadvan cfscore_ideologue gender termlimit i.yearDT if priuo!=1, cluster(bonicaridDT)

* Table A10
* MODEL A10.1: 
firthlogit priwin expectation maxqualchall pripct_lag incumptyadvan cfscore_ideologue gender power allleader termlimit i.yearDT
* MODEL A10.2: 
firthlogit priwin i.expectation maxqualchall pripct_lag incumptyadvan cfscore_ideologue gender power allleader termlimit i.yearDT 
* MODEL A10.3: 
firthlogit priwin les maxqualchall pripct_lag incumptyadvan cfscore_ideologue gender power allleader termlimit i.yearDT 
* MODEL A10.4: 
firthlogit priwin expectation maxqualchall pripct_lag incumptyadvan cfscore_ideologue gender power allleader termlimit i.yearDT if priuo!=1

* Table A11
* MODEL A11.1: 
xtlogit priwin expectation maxqualchall pripct_lag incumptyadvan cfscore_ideologue gender power allleader termlimit i.yearDT
* MODEL A11.2:
xtlogit priwin i.expectation maxqualchall pripct_lag incumptyadvan cfscore_ideologue gender power allleader termlimit i.yearDT 
* MODEL A11.3:
xtlogit priwin les maxqualchall pripct_lag incumptyadvan cfscore_ideologue gender power allleader termlimit i.yearDT 
* MODEL A11.4: 
xtlogit priwin expectation maxqualchall pripct_lag incumptyadvan cfscore_ideologue gender power allleader termlimit i.yearDT if priuo!=1

* Table A12
* MODEL A2.1: 
logit priwin expectation maxqualchall pripct_lag incumptyadvan cfscore_ideologue gender power allleader termlimit i.yearDT if yearDT<1995, cluster(bonicaridDT)
* MODEL A12.2: 
logit priwin expectation maxqualchall pripct_lag incumptyadvan cfscore_ideologue gender power allleader termlimit i.yearDT if yearDT>1995, cluster(bonicaridDT)

* Table A13
* MODEL A13.1: 
firthlogit priwin c.expectation##c.nchallengers_sameparty pripct_lag incumptyadvan cfscore_ideologue gender power allleader termlimit i.yearDT 
* MODEL A13.2:
firthlogit priwin i.expectation##c.nchallengers_sameparty pripct_lag incumptyadvan cfscore_ideologue gender power allleader termlimit i.yearDT 
* MODEL A13.3:
firthlogit priwin c.les##c.nchallengers_sameparty pripct_lag incumptyadvan cfscore_ideologue gender power allleader termlimit i.yearDT 
* MODEL A13.4:
firthlogit priwin c.expectation##c.nchallengers_sameparty pripct_lag incumptyadvan cfscore_ideologue gender power allleader termlimit i.yearDT if priuo!=1


